package com.yimi.yinhepda.downLoadBaseData;

import android.content.Context;
import android.widget.Toast;

import com.pda.ym.callbacks.UICallBack;
import com.yimi.yinhepda.db.dao.OffLineLoadUnloadGroupTableDao;
import com.yimi.yinhepda.db.dao.OffLinePlatformNoTableDao;
import com.yimi.yinhepda.db.dao.UcWaybillRegexTableDao;
import com.yimi.yinhepda.db.dao.UserInfoTableDao;
import com.yimi.yinhepda.db.table.UcWaybillRegexTable;
import com.yimi.yinhepda.entity.bean.LoadUnloadGroupEntity;
import com.yimi.yinhepda.entity.bean.UserInfoEntity;
import com.yimi.yinhepda.entity.request.LoadUnloadEmpRequest;
import com.yimi.yinhepda.entity.request.NoDataRequest;
import com.yimi.yinhepda.entity.response.LoadUnLoadGroupResponse;
import com.yimi.yinhepda.entity.response.PlatformNoResponse;
import com.yimi.yinhepda.entity.response.QueryUcWaybillRegexResponse;
import com.yimi.yinhepda.entity.response.QueryUsersAndDeptResponse;
import com.pda.ym.net.NetRequest;
import com.yimi.yinhepda.utils.Common;
import com.pda.ym.utils.Constants;
import com.pda.ym.utils.DateHelper;
import com.pda.uce.commom.util.LogUtil;
import com.pda.ym.utils.SharedPreferUtil;
import com.yimi.yinhepda.utils.ToastUtil;
import com.yimi.yinhepda.utils.UcWaybillValidate;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 下载离线数据.
 *
 * @author lj
 */
public class DownLoadForOffLineData extends UICallBack {

    private Context context;
    private SharedPreferUtil sharedPreferUtil = SharedPreferUtil.getInstance();

    public DownLoadForOffLineData(Context context) {
        this.context = context;
    }

    /**
     * 开始获取月台号数据数据.
     */
    public void startDownLoadPlatformNoData() {
        //请求月台号接口
        LogUtil.e("下载月台号开始--->" + DateHelper.getDateTimeFormate(new Date()));
        Map<String, String> params = new HashMap<>(0);
        params.put("deptCode", sharedPreferUtil.getDeptCode());
        new NetRequest().setMethod(NetRequest.Method.GET)
                .setUrl(Constants.URL_QUERYPLATFORMNO)
                .setParams(params)
                .setResponsClazz(PlatformNoResponse.class)
                .execute(this);
    }

    /**
     * 获取装卸组数据
     */
    private void downLoadGroupData() {
        LogUtil.e("下载装卸组开始--->" + DateHelper.getDateTimeFormate(new Date()));
        LoadUnloadEmpRequest empRequest = new LoadUnloadEmpRequest();
        empRequest.setDeptCode(sharedPreferUtil.getDeptCode());
        new NetRequest().setMethod(NetRequest.Method.POST_STRING)
                .setUrl(Constants.URL_QUERY_LOAD_TEAM)
                .setRequestObject(empRequest)
                .setResponsClazz(LoadUnLoadGroupResponse.class)
                .execute(this);
    }

    /**
     * 获取部门所有用户
     */
    private void downLoadUser() {
        LogUtil.e("下载部门所有用户开始--->" + DateHelper.getDateTimeFormate(new Date()));
        Map<String, String> params = new HashMap<>(0);
        params.put("userCode", sharedPreferUtil.getValue(Common.USER_CODE));
        new NetRequest().setMethod(NetRequest.Method.GET)
                .setUrl(Constants.URL_QUERY_USER_AND_DEPT)
                .setParams(params)
                .setResponsClazz(QueryUsersAndDeptResponse.class)
                .execute(this);
    }

    /**
     * 获取快递运单规则
     */
    private void downLoadUcWaybillRegex() {
        LogUtil.e("下载快递运单规则开始--->" + DateHelper.getDateTimeFormate(new Date()));
        NoDataRequest request = new NoDataRequest();
        new NetRequest().setMethod(NetRequest.Method.POST_STRING)
                .setUrl(Constants.URL_UC_PAD_TASK)
                .setRequestObject(request)
                .setResponsClazz(QueryUcWaybillRegexResponse.class)
                .execute(this);
    }


    @Override
    public void onError(String msg) {
        ToastUtil.showToast(msg);
        LogUtil.e(msg);
    }

    @Override
    public void onSuccess(Object obj) {
        if (obj instanceof PlatformNoResponse) {
            LogUtil.e("插入月台号开始--->" + DateHelper.getDateTimeFormate(new Date()));
            PlatformNoResponse response = (PlatformNoResponse) obj;
            if (response.getData() != null) {
                new OffLinePlatformNoTableDao().createOrUpdate(sharedPreferUtil.getDeptCode(), response.getData());
            }
            LogUtil.e("下载月台号结束--->" + DateHelper.getDateTimeFormate(new Date()));
            //下载装卸组
            downLoadGroupData();
        } else if (obj instanceof LoadUnLoadGroupResponse) {
            LogUtil.e("插入装卸组开始--->" + DateHelper.getDateTimeFormate(new Date()));
            LoadUnLoadGroupResponse response = (LoadUnLoadGroupResponse) obj;
            if (response.getData() != null) {
                String teamCodes = response.getData().getTeamCode();
                String teamNames = response.getData().getTeamName();
                List<LoadUnloadGroupEntity> groupEntityList = response.getData().getEmps();
                new OffLineLoadUnloadGroupTableDao().createOrUpdate(sharedPreferUtil.getDeptCode(), teamCodes, teamNames, groupEntityList);
            }
            LogUtil.e("下载装卸组结束--->" + DateHelper.getDateTimeFormate(new Date()));
            //下载部门所有用户
            downLoadUser();
        } else if (obj instanceof QueryUsersAndDeptResponse) {
            LogUtil.e("插入部门所有用户开始--->" + DateHelper.getDateTimeFormate(new Date()));
            QueryUsersAndDeptResponse response = (QueryUsersAndDeptResponse) obj;
            if (response.getData() != null) {
                List<UserInfoEntity> userInfoEntityList = response.getData().getUserList();
                new UserInfoTableDao().createOrUpdate(userInfoEntityList);
            }
            LogUtil.e("下载部门所有用户结束--->" + DateHelper.getDateTimeFormate(new Date()));
            //获取快递运单规则
            downLoadUcWaybillRegex();
        } else if (obj instanceof QueryUcWaybillRegexResponse) {
            LogUtil.e("插入快递运单规则开始--->" + DateHelper.getDateTimeFormate(new Date()));
            QueryUcWaybillRegexResponse response = (QueryUcWaybillRegexResponse) obj;
            if (response.getResult() != null) {
                UcWaybillValidate.clearData();
                List<UcWaybillRegexTable> list = response.getResult();
                new UcWaybillRegexTableDao().createOrUpdate(list);
            }
            LogUtil.e("下载快递运单规则结束--->" + DateHelper.getDateTimeFormate(new Date()));
        }
    }

}
